from django.contrib.auth import get_user_model
from rest_framework import serializers

from account.models import FocusUser

User = get_user_model()


class UserForeignKeySer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = (
            'id',
            'username',
            'phone',
            'email',
            'gender',
            'portrait',
            'signature',
            'expertise_area',
        )


class UserCreateSer(serializers.ModelSerializer):
    password = serializers.CharField(max_length=30, write_only=True)

    class Meta:
        model = User
        fields = (
            'id',
            'username',
            'phone',
            'email',
            'gender',
            'portrait',
            'signature',
            'password',
            'expertise_area',
        )

    def create(self, validated_data):
        password = validated_data.pop('password')
        user = User.objects.create(**validated_data)
        if password:
            user.set_password(password)
            user.save()

        return user


class UserUpdateSer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = (
            'id',
            'username',
            'phone',
            'email',
            'gender',
            'portrait',
            'signature',
            'expertise_area',
        )


class UserDeleteSer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = (
        )


class UserDetailSer(serializers.ModelSerializer):
    # from questions.serializer import QuestionForeignKeySer
    # from answer.serializer import AnswerForeignKeySer
    # from comment.serializer import CommentForeignKeySer
    #
    # answers = AnswerForeignKeySer(many=True, read_only=True)
    # questions = QuestionForeignKeySer(many=True, read_only=True)
    # comments = CommentForeignKeySer(many=True, read_only=True)
    expertise_area = serializers.SerializerMethodField()

    def get_expertise_area(self,obj):
        if obj.expertise_area is not None:
            return obj.expertise_area.name
        return None

    class Meta:
        model = User
        fields = (
            'id',
            'username',
            'phone',
            'email',
            'gender',
            'portrait',
            'signature',
            'expertise_area',

            # # foreign key
            # 'comments',
            # 'answers',
            # 'questions',
        )


class UserFocusListSer(serializers.ModelSerializer):
    focused = UserForeignKeySer(read_only=True)

    class Meta:
        model = FocusUser
        fields = ('id', 'focused',)


class UserFocusedListSer(serializers.ModelSerializer):
    creator = UserForeignKeySer(read_only=True)

    class Meta:
        model = FocusUser
        fields = ('id', 'creator',)


class UserFocusCreateSer(serializers.ModelSerializer):
    creator = UserForeignKeySer(
        default=serializers.CurrentUserDefault()
    )
    focused = UserForeignKeySer(read_only=True)

    class Meta:
        model = FocusUser

        fields = ('id', 'focused', 'creator',)

class UserFocusDeletSer(serializers.ModelSerializer):
    class Meta:
        model = FocusUser
        fields = (
        )